#include "mainwindow.h"

#include <QApplication>
#include <QElapsedTimer>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QTextCodec>
#include <QTranslator>
#include <QDateTime>
#include <QSettings>
#include <QDateTime>

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static int logflag = -1;
    if(logflag<0){
        QDir dir(QCoreApplication::applicationDirPath());
        dir.cdUp();
        dir.cd("config");
        if(!dir.exists("system.ini"))return;
        QSettings setting(dir.absoluteFilePath(dir.absoluteFilePath("system.ini")),QSettings::IniFormat);

        setting.beginGroup("system");
        if(!setting.allKeys().contains("logflag"))setting.setValue("logflag",int(15));
        logflag = setting.value("logflag").toInt();
        setting.endGroup();
    }

    QByteArray localMsg = msg.toLocal8Bit();

    // 不输出任何数据，仅在控制台显示
    if(logflag < 1){
        switch (type) {
        case QtDebugMsg:
            fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
            break;
        case QtInfoMsg:
            fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
            break;
        case QtWarningMsg:
            fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
            break;
        case QtCriticalMsg:
            fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
            break;
        case QtFatalMsg:
            fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
            break;
        }
    }
    else{
        QDir logDir(QCoreApplication::applicationDirPath());
        logDir.cdUp();
        if(!logDir.exists("log"))logDir.mkdir("log");
        logDir.cd("log");
        QFile logFile(logDir.absoluteFilePath(QDateTime::currentDateTime().toString("yyyy-MM-dd-hh")+".log"));
        if(!logFile.open(QIODevice::Append))return;
        QTextStream out(&logFile);
        if((logflag & QtDebugMsg) == QtDebugMsg && type == QtDebugMsg){
            out<<QString("Debug:%1,%2,%3,%4\r\n").arg(QDateTime::currentDateTime().toString("hh:mm:ss")).arg(localMsg.constData()).arg(context.file).arg(context.line);
        }

        if((logflag & QtInfoMsg) == QtInfoMsg && type == QtInfoMsg){
            out<<QString("Info:%1,%2,%3,%4\r\n").arg(QDateTime::currentDateTime().toString("hh:mm:ss")).arg(localMsg.constData()).arg(context.file).arg(context.line);
        }

        if((logflag & QtWarningMsg) == QtWarningMsg && type == QtWarningMsg){
            out<<QString("Warning:%1,%2,%3,%4\r\n").arg(QDateTime::currentDateTime().toString("hh:mm:ss")).arg(localMsg.constData()).arg(context.file).arg(context.line);
        }

        if((logflag & QtCriticalMsg) == QtCriticalMsg && type == QtCriticalMsg){
            out<<QString("Critical:%1,%2,%3,%4\r\n").arg(QDateTime::currentDateTime().toString("hh:mm:ss")).arg(localMsg.constData()).arg(context.file).arg(context.line);
        }

        if((logflag & QtFatalMsg) == QtFatalMsg && type == QtFatalMsg){
            out<<QString("Fatal:%1,%2,%3,%4\r\n").arg(QDateTime::currentDateTime().toString("hh:mm:ss")).arg(localMsg.constData()).arg(context.file).arg(context.line);
        }
        logFile.close();
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
#if 1
    // 初始化样式
    QFile qssFile;
    qssFile.setFileName(":/skin/gray.qss");
    if(qssFile.open(QIODevice::Text | QIODevice::ReadOnly))
    {
        QString m_strQssGrey = qssFile.readAll();
//        QApplication::setStyle(m_strQssGrey);
        qApp->setStyleSheet(m_strQssGrey);

    }
    qssFile.close();
#endif
    QFont f = a.font();

    f.setFamily("Microsoft YaHei");
    a.setFont(f);

    QDir::setCurrent(a.applicationDirPath());

    // 打开日志重定向
    //    qInstallMessageHandler(myMessageOutput);

#if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

#else
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));

#endif

    QDir dir(QApplication::applicationDirPath());
    dir.cdUp();
    dir.cd("translate");
    QFileInfoList fileList = dir.entryInfoList(QStringList()<<"*.qm",QDir::Files);
    foreach(QFileInfo fileInfo,fileList)
    {
        QTranslator* translator = new QTranslator();
        bool bFlag = translator->load(fileInfo.fileName(),dir.absolutePath());
        if (bFlag == true)
        {
            a.installTranslator(translator);
        }
    }

    MainWindow w;
    w.showMaximized();
    return a.exec();
}
